package com.tensquare.base.service;

import com.tensquare.base.dao.CityDao;
import com.tensquare.base.pojo.City;
import com.tensquare.base.pojo.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Alfred
 * @description TODO
 * @date 2021/4/26 19:41
 */
@Service
public class CityService {
    @Autowired
    private CityDao cityDao;
    @Autowired
    private IdWorker idWorker;

    /**
     * fetch data by rule id
     *
     * @description  增加城市
     * @param
     * @return
     */
    public void add(City city){
        city.setId(idWorker.nextId()+"");
        cityDao.save(city);
    }

    /**
     * fetch data by rule id
     *
     * @description  查询城市列表
     * @param
     * @return
     */
    public List<City> findAll(){
        return cityDao.findAll();
    }

    /**
     * fetch data by rule id
     *
     * @description  修改城市
     * @param
     * @return
     */
    public void update(City city){
        cityDao.save(city);
    }

    /**
     * fetch data by rule id
     *
     * @description  删除城市
     * @param
     * @return
     */
    public void delete(String id){
        cityDao.deleteById(id);
    }

    /**
     * fetch data by rule id
     *
     * @description  按ID查询城市
     * @param
     * @return
     */
    public City findById(String id){
        return cityDao.findById(id).get();
    }

    /**
     * fetch data by rule id
     *
     * @description  构建查询条件
     * @param
     * @return
     */
    public Specification<City> createSpecification(Map searchMap){
        return new Specification<City>(){
            @Override
            public Predicate toPredicate(Root<City> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<>();
                if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                    predicateList.add(cb.like(root.get("name").as(String.class),"%"+(String) searchMap.get("name")+"%"));
                }
                if(searchMap.get("ishot")!=null && !"".equals(searchMap.get("ishot"))){
                    predicateList.add(cb.equal(root.get("ishot").as(String.class),(String) searchMap.get("ishot")));
                }
                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    /**
     * fetch data by rule id
     *
     * @description  按条件查询城市
     * @param
     * @return
     */
    public List<City> findSearch(Map searchMap){
        Specification<City> specification = createSpecification(searchMap);
        return cityDao.findAll(specification);
    }

    /**
     * fetch data by rule id
     *
     * @description  分页查询城市
     * @param
     * @return
     */
    public Page<City> findSearch(Map searchMap,int page,int size){
        Specification<City> specification = createSpecification(searchMap);
        PageRequest pageList = PageRequest.of(page-1, size);
        return cityDao.findAll(specification,pageList);
    }
}
